#ifndef __GUI_HPP
#define __GUI_HPP

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

void create_ui();

template <typename IT>
std::vector<cv::Point> get_points(IT begin, IT end, double xmin, double xmax, double ymin, double ymax);

template <typename IT>
void show_ui(cv::Mat &image, IT hr_begin, IT hr_end, IT spo2_begin, IT spo2_end);

template <typename IT>
std::vector<cv::Point> get_points(IT begin, IT end, double xmin, double xmax, double ymin, double ymax) {
    int n = end - begin;
    auto y_normalized = normalized(begin, end, ymin, ymax);
    std::vector<cv::Point> ans;
    for (int i=0; i<n; ++i) {
        int x = int(xmin + (xmax - xmin)*i/n);
        ans.push_back(cv::Point(x, (ymax + ymin - y_normalized[i]))*2);
    }
    return ans;
}

template <typename IT>
void show_ui(cv::Mat& image, IT hr_begin, IT hr_end, IT spo2_begin, IT spo2_end)
{
    if (hr_begin != hr_end) {
        double hr = *(hr_end - 1); 
        cv::putText(image, "heartrate: " + std::to_string(hr), cv::Point(0, 25), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 1, 8);
    }
    if (hr_end-hr_begin > 1) {
        cv::polylines(image,get_points(hr_begin, hr_end, 20.0, 280.0, 30.0, 230.0),false,cv::Scalar(0, 0, 255),1,8,1);
    }
    
    if (spo2_begin != spo2_end) {
        double spo2 = *(spo2_end - 1);
        cv::putText(image, "Spo2: " + std::to_string(spo2), cv::Point(300, 25), cv::FONT_HERSHEY_SIMPLEX, 0.7, cv::Scalar(0, 0, 255), 1, 8);
    }
    if (spo2_end-spo2_begin > 1) {
        cv::polylines(image,get_points(spo2_begin, spo2_end, 320.0, 580.0, 30.0, 230.0),false,cv::Scalar(0, 0, 255),1,8,1);
    }

    cv::imshow("show_data", image);
}

#endif // __GUI_HPP
